package com.example.yls.demoa;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
private Button start;
private Button stop;
private ImageView imgv;
private AnimationDrawable animationDrawable;
private ImageView img1;
private Button btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        animationDrawable= (AnimationDrawable) imgv.getDrawable();

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animationDrawable.start();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  animationDrawable.stop();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //AlphaAnimationEvent();
                TranslateAnimationEvent();
                ScaleAnimationEvent();

            }
        });
    }
    private void ScaleAnimationEvent() {
        /* ScaleAnimation alphaAnimation= (ScaleAnimation) AnimationUtils.loadAnimation
                        (MainActivity.this,
                                R.anim.scale
                );
                img1.startAnimation(alphaAnimation);*/

        ScaleAnimation alphaAnimation=new ScaleAnimation(1,4,1,4);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatCount(2);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        img1.startAnimation(alphaAnimation);

    }

    private void TranslateAnimationEvent() {

 TranslateAnimation alphaAnimation= (TranslateAnimation) AnimationUtils.loadAnimation
                        (MainActivity.this,
                                R.anim.tran
                );
                img1.startAnimation(alphaAnimation);

      /*  TranslateAnimation alphaAnimation=new TranslateAnimation
                (0,300,0,300);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatCount(2);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        img1.startAnimation(alphaAnimation);*/

    }

    private void AlphaAnimationEvent() {
     /* AlphaAnimation alphaAnimation= (AlphaAnimation) AnimationUtils.loadAnimation
                        (MainActivity.this,
                                R.anim.alpah
                );
                img1.startAnimation(alphaAnimation);*/

        AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatCount(2);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        img1.startAnimation(alphaAnimation);
    }

    private void initView() {

         start=findViewById(R.id.btn1);
         stop=findViewById(R.id.btn2);
         imgv=findViewById(R.id.img_v);
         img1=findViewById(R.id.img1);
         btn3=findViewById(R.id.btn3);


    }

}
